<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> VIP服务管理
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container v2-group">
            <div class="handle-box">
                <el-input v-model="query.kw" placeholder="关键词" class="handle-input mr10" clearable></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">查询</el-button>
                <el-button
                    type="success"
                    icon="el-icon-plus"
                    class="handle-del mr10"
                    @click="handleAdd"
                >新增VIP</el-button>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
                stripe
            >
                <!-- <el-table-column type="selection" width="55" align="center"></el-table-column> -->
                <el-table-column prop="name" label="服务名称"></el-table-column>
                <el-table-column prop="tar" label="使用平台"></el-table-column>
                <el-table-column prop="price" label="金额"></el-table-column>
                <el-table-column prop="time" label="服务时长"></el-table-column>
                <el-table-column prop="manager" label="管理者"></el-table-column>
                <el-table-column label="更新时间">
                    <template slot-scope="scope">{{scope.row.update_time}}</template>
                </el-table-column>
                <el-table-column label="操作" width="210">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, sizes, prev, pager, next, jumper"
                    :current-page="query.page"
                    :page-size="query.size"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                    :page-sizes="[10, 20, 30, 40, 50, 100]"
                    @size-change="handleSizeChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="entityEdit" :model="form" :rules="rulesEntityEdit" label-width="90px">
                <el-form-item label="服务类型" prop="vip_type">
                    <el-input v-model="form.vip_type"></el-input>
                </el-form-item>
                <el-form-item label="名称" prop="name">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="使用平台" prop="tar">
                    <el-input v-model="form.tar"></el-input>
                </el-form-item>
                <el-form-item label="金额" prop="price">
                    <el-input v-model="form.price"></el-input>
                </el-form-item>
                <el-form-item label="服务时长" prop="time">
                    <el-input v-model="form.time"></el-input>
                </el-form-item> 
                <el-form-item label="管理者" prop="manager">
                    <el-input v-model="form.manager"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 新增弹出框 -->
        <el-dialog title="新增" :visible.sync="addVisible" width="30%">
            <el-form ref="entityAdd" :model="form" :rules="rulesEntityAdd" label-width="90px">
                <el-form-item label="服务类型" prop="vip_type">
                    <el-input v-model="form.vip_type"></el-input>
                </el-form-item>
                <el-form-item label="名称" prop="name">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="使用平台" prop="tar">
                    <el-input v-model="form.tar"></el-input>
                </el-form-item>
                <el-form-item label="金额" prop="price">
                    <el-input v-model="form.price"></el-input>
                </el-form-item>
                <el-form-item label="服务时长" prop="time">
                    <el-input v-model="form.time"></el-input>
                </el-form-item> 
                <el-form-item label="管理者" prop="manager">
                    <el-input v-model="form.manager"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveAdd">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import api from '../../../api/index';
export default {
    name: 'basetable',
    data() {
        return {
            query: {
                vip_type: '',
                kw: '',
                page: 1,
                size: 10
            },
            tableData: [
                {
                    username: 'user01',
                    surname: '张三',
                    user_group: '普通用户',
                    last_login: '2024-11-01 00:00:00',
                    status: '正常'
                }
            ],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            pageTotal: 0,
            form: {},
            idx: -1,
            id: -1,
            addVisible: false,
            roles: [],
            rulesEntityAdd: {
                vip_type: [{ required: true, message: '请输入服务类型', trigger: 'blur' }],
                name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
                tar: [{ required: true, message: '请输入使用平台', trigger: 'blur' }],
                price: [{ required: true, message: '请输入金额', trigger: 'blur' }],
                time: [{ required: true, message: '请输入服务时长', trigger: 'blur' }],
                manager: [{ required: true, message: '请输入管理者', trigger: 'blur' }]
            },
            rulesEntityEdit: {
                vip_type: [{ required: true, message: '请输入服务类型', trigger: 'blur' }],
                name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
                tar: [{ required: true, message: '请输入使用平台', trigger: 'blur' }],
                price: [{ required: true, message: '请输入金额', trigger: 'blur' }],
                time: [{ required: true, message: '请输入服务时长', trigger: 'blur' }],
                manager: [{ required: true, message: '请输入管理者', trigger: 'blur' }]
            }
        };
    },
    created() {
        this.getData();
    },
    methods: {
        // 获取 easy-mock 的模拟数据
        getData() {
            api.getVipList(this.query).then(res => {
                this.tableData = res.items;
                this.pageTotal = res.total;
            }).catch(err => {
                this.$message.error(err);
            });
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'page', 1);
            this.getData();
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
            .then(() => {
                api.deleteVip({pk: row.id}).then(res => {
                    this.$message.success('删除成功');
                    this.getData();
                }).catch(err => {
                    this.$message.error(err);
                });
            })
            .catch(() => {});
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].name + ' ';
            }
            this.$message.error(`删除了${str}`);
            this.multipleSelection = [];
        },
        // 编辑操作
        handleEdit(index, row) {
            this.idx = index;
            this.form = Object.assign({}, row);
            // this.form.user_group = row.user_role;
            this.editVisible = true;
            this.loadDetail();
        },
        // 保存编辑
        saveEdit() {
            this.$refs.entityEdit.validate(valid => {
                if (valid) {
                    var param = Object.assign({}, this.form, {pk: this.form.id});
                    delete param.id;
                    delete param.update_time;
                    api.editVip(param).then(res => {
                        this.editVisible = false;
                        this.$message.success(`修改第 ${this.idx + 1} 行成功`);
                        // this.$set(this.tableData, this.idx, this.form);
                        this.getData();
                    }).catch(err => {
                        this.$message.error(err);
                    });
                } else {
                    return false;
                }
            });
        },
        // 新增操作
        handleAdd() {
            this.form = {};
            this.addVisible = true;
            // this.loadRoles();
        },
        saveAdd() {
            this.$refs.entityAdd.validate(valid => {
                if (valid) {
                    api.addVip(Object.assign({}, this.form)).then(res => {
                        this.editVisible = false;
                        this.$message.success(`新增成功`);
                        this.addVisible = false;
                        this.getData();
                    }).catch(err => {
                        this.$message.error(err);
                    });
                } else {
                    return false;
                }
            });
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'page', val);
            this.getData();
        },
        handleSizeChange(val) {
            this.$set(this.query, 'size', val);
            this.getData();
        },
        loadDetail() {
            api.getRoles(this.query).then(res => {
                this.roles = res.items;
            }).catch(err => {
                this.$message.error(err);
            });
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
